package org.chen.newsapi.controller;

import org.chen.newsapi.common.Page;
import org.chen.newsapi.common.Result;
import org.chen.newsapi.dto.ArticleDetailVO;
import org.chen.newsapi.dto.ArticleIndex;
import org.chen.newsapi.dto.CategoryVO;
import org.chen.newsapi.dto.UserArticleQueryParam;
import org.chen.newsapi.service.ArticleService;
import org.chen.newsapi.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/apis/news")
public class ArticleController {
    
    @Autowired
    private ArticleService articleService;

    @Autowired
    private CategoryService categoryService;
    
    @PostMapping("/articles")
    public Page<ArticleIndex> getArticles(@RequestBody UserArticleQueryParam param) {
        return articleService.getArticles(param);
    }
    
    @GetMapping("/articles/{id}")
    public Result<ArticleDetailVO> getArticleDetail(@PathVariable String id) {
        return articleService.getArticleDetail(id);
    }

    @GetMapping("/categories")
    public Result<List<CategoryVO>> getCategories() {
        try {
            List<CategoryVO> categories = categoryService.getAllCategories();
            return Result.success(categories);
        } catch (Exception e) {
            return Result.error("获取分类失败: " + e.getMessage());
        }
    }
}